/**********************************************************************
 *                                                                    *
 * tgt - Tiny Graphics Toolbox                                        *
 *                                                                    *
 * Copyright (C) 2006-2008 Visualization and Computer Graphics Group, *
 * Department of Computer Science, University of Muenster, Germany.   *
 * <http://viscg.uni-muenster.de>                                     *
 *                                                                    *
 * This file is part of the tgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

#include "tgt/shadermanager.h"

#include "tgt/quadric.h"
#include "tgt/memorymanager.h"
#include "tgt/toolkitfactory.h"
#include "tgt/guiapplication.h"
#include "tgt/painter.h"
#include "tgt/camera.h"

using namespace tgt;

class MyPainter : public Painter {
   public:
    MyPainter(GLCanvas* canvas) : Painter(canvas), shader_(0) {};
    ~MyPainter();
    virtual void init();
    virtual void paint();
    virtual void sizeChanged(const ivec2& size);

private:
    Shader* shader_;
};

MyPainter::~MyPainter() {
    delete shader_;
}

void MyPainter::init() {
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    
    // set Camera
    getCamera()->setPosition(vec3(0.f,0.f,4.f));
    getCamera()->look();
    
    glColor4f(1.f, 0.f, 0.f, 1.f);
    
    ShdrMgr.addPath("data/shaders");
	ShdrMgr.addPath("../samples/data/shaders");
    
	shader_ = ShdrMgr.loadSeparate("myshader.vert","myshader.frag");
}

void MyPainter::paint() {

    // render sphere
    if (shader_) {
        shader_->activate();
        Sphere sphere(1.f, 16, 8);
        sphere.render();
        shader_->deactivate();
	} else std::cout << "No shader available." << std::endl;
}

void MyPainter::sizeChanged(const ivec2& size) {
    glViewport(0,0,size.x,size.y);
    getCamera()->setRatio((float) size.x/size.y);
}


int main(int argc, char** argv) {

    std::cout << "tgt Sample Program: shader" << std::endl
              << std::endl
              << "Demonstrates loading of shaders in tgt." << std::endl
              << std::endl;
    
    GUIApplication* tgtApp = ToolkitFactory::createApplication(argc, argv);

    GLCanvas* GLCanvas = ToolkitFactory::createCanvas("tgt Example: Shader");
    tgtApp->addCanvas(GLCanvas);

    tgtApp->init();

    Camera camera;
    GLCanvas->setCamera(&camera);

    MyPainter painter(GLCanvas);
    GLCanvas->setPainter(&painter);

    tgtApp->run();

    delete GLCanvas;
    delete tgtApp;

    return 0;
}
